﻿using System;
using System.Collections.Generic;
using System.Text;
using Mighty.Common;

namespace Mighty.Brain
{
    public class UnderBrain : BasicBrain
    {
        static int BBcnt = 0;
        static Giruda[] Strong = new Giruda[5];
        List<Card>[] property = new List<Card>[4];

        private List<HandedCard> myproper(List<HandedCard> hand, int asc)
        {
            List<HandedCard> pro = new List<HandedCard>();
            HandedCard win = new HandedCard();

            int i, j;

            for (i = 0; i < hand.Count; i++)
            {
                win = hand[0];

                foreach (HandedCard nxt in hand)
                {
                    if (nxt == gameInfo.mighty)
                    {
                        pro.Add(nxt);
                        hand.Remove(nxt);
                        break;
                    }
                    if (nxt.shape == Shape.Joker && gameInfo.roundIdx != 0)
                    {
                        pro.Add(nxt);
                        hand.Remove(nxt);
                        break;
                    }
                    if (nxt.shape == (Shape)gameInfo.betting.giruda)
                    {
                        if (win.shape == (Shape)gameInfo.betting.giruda)
                        {
                            if (win.number < nxt.number)
                            {
                                win = nxt;
                            }
                        }
                        else
                        {
                            win = nxt;
                        }
                    }
                    else if (win.shape != (Shape)gameInfo.betting.giruda)
                    {
                        if (win.number < nxt.number)
                            win = nxt;
                    }
                }
                pro.Add(win);
                hand.Remove(win);
            }
            if (asc == 1)
            {
                return pro;
            }
            else
            {
                hand.Clear();
                for (i = pro.Count - 1; i >= 0; i--)
                {
                    hand.Add(pro[i]);
                }
                return hand;
            }
        }
        private List<HandedCard> myproper(List<HandedCard> hand)
        {
            return myproper(hand, 1);
        }

        private List<HandedCard> mypic(List<HandedCard> hand, int asc)
        {
            List<HandedCard> pro = new List<HandedCard>();
            HandedCard win = new HandedCard();

            int i, j;

            for (i = 0; i < hand.Count; i++)
            {
                win = hand[0];

                foreach (HandedCard nxt in hand)
                {
                    if (nxt == gameInfo.mighty)
                    {
                        pro.Add(nxt);
                        hand.Remove(nxt);
                        break;
                    }
                    if (nxt.shape == Shape.Joker && gameInfo.roundIdx != 0)
                    {
                        pro.Add(nxt);
                        hand.Remove(nxt);
                        break;
                    }
                    if (nxt.shape == (Shape)gameInfo.betting.giruda)
                    {
                        if (win.shape == (Shape)gameInfo.betting.giruda)
                        {
                            if (win.number < nxt.number)
                            {
                                win = nxt;
                            }
                        }
                        else
                        {
                            win = nxt;
                        }
                    }
                    else if (win.shape != (Shape)gameInfo.betting.giruda)
                    {
                        if (win.number < nxt.number)
                            win = nxt;
                    }
                }
                if (win.number > 9) pro.Add(win);
                hand.Remove(win);
            }
            if (asc == 1)
            {
                return pro;
            }
            else
            {
                hand.Clear();
                for (i = pro.Count - 1; i >= 0; i--)
                {
                    hand.Add(pro[i]);
                }
                return hand;
            }
        }

        private List<HandedCard> mypic(List<HandedCard> hand)
        {
            return mypic(hand, 1);
        }

        private bool attackhand()
        {
            int i;
            for (i = 0; i < gameInfo.roundCardIdx; i++)
            {
                if (i - gameInfo.roundCardIdx + gameInfo.myInfo.position == gameInfo.attackPlayer.position) return true;
            }
            return false;
        }

        private bool friendhand()
        {
            int i;
            for (i = 0; i < gameInfo.roundCardIdx; i++)
            {
                if (i - gameInfo.roundCardIdx + gameInfo.myInfo.position == gameInfo.friend.position) return true;
            }
            return false;
        }

        private bool friendOut()
        {
            if (gameInfo.friend.name == "") return true;
            return false;
        }

        private int eatn()
        {
            int i, j=0;

            for (i = 0; i < gameInfo.roundCardIdx; i++)
            {
                if (gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i].number > 9) j++;
            }
            return j;
        }

        private int whowin()
        {
            int i,j=0;
            Card win = new Card();

            win = gameInfo.roundHistory[gameInfo.roundIdx].cardStack[0];
            if (win.shape == Shape.Joker) {
                win.shape = gameInfo.roundHistory[gameInfo.roundIdx].cardStack[0].jokerTarget;
                win.number = 14;
            }



            for (i = 1; i < gameInfo.roundCardIdx;i++ )
            {
                if ((Card)gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i] == gameInfo.mighty)
                    return i;
                if (gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i].shape == Shape.Joker
                    && !gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i].isJokerCall)
                    return i;




                if (gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i].shape ==
                    (Shape)gameInfo.betting.giruda)
                {

                    if (win.shape != (Shape)gameInfo.betting.giruda)
                    {
                        win = gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i];
                        j = i;
                    }
                    else
                    {
                        if (win.number < gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i].number)
                        {
                            win = gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i];
                            j = i;
                        }
                    }
                }
                else
                {
                    if (win.shape != (Shape)gameInfo.betting.giruda &&
                        gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i].shape == win.shape)
                    {
                        if (gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i].number > win.number)
                        {
                            win = gameInfo.roundHistory[gameInfo.roundIdx].cardStack[i];
                            j = i;
                        }
                    }
                }
            }

            return j-gameInfo.roundCardIdx+gameInfo.myInfo.position;
        }

        public override void GameStartEvent()
        {
            int i, j;
            for (i = 0; i < 5; i++)
            {
                if (gameInfo.players[i].name == "BasicBrain") BBcnt++;
            }
            Card tmp = new Card();
            for (i=0; i <4 ; i++)
            {
                tmp.shape = (Shape)i;
                for (j=14; j>1; j--)
                {
                    tmp.number = j;
                    //property[i].Add(tmp);
                }
            }
        }

        public override Betting BettingTicEvent()
        {
            Betting bet = new Betting();

            bet.isPass = true;

            return bet;
        }

        public override void BettingCompleteEvent()
        {
            
            for (int i=0;i<gameInfo.bettingHistory.Count;i++) {
                if (!gameInfo.bettingHistory[i].isPass && (gameInfo.bettingHistory[i].limit > 14))
                {
                    Strong[i]=gameInfo.bettingHistory[i].giruda;
                }
                else Strong[i]=(Giruda.NoGiruda);
            }
            return;
        }

        public override HandedCard RoundTicEvent()
        {
            List<HandedCard> pos = new List<HandedCard>();
            foreach (HandedCard card in gameInfo.myCards)
            {
                if (card.isPossible) pos.Add(card);
            }
            
            if (gameInfo.myInfo.position != gameInfo.attackPlayer.position && gameInfo.myInfo.position != gameInfo.friend.position)
            { // 수비플레이어일때

                if (friendOut() && !attackhand() && !friendhand()
                    && eatn() > 1 && whowin() != gameInfo.friend.position && whowin() != gameInfo.attackPlayer.position)
                {
                    return myproper(pos)[0];
                }
            }
            return myproper(pos, 0)[0];
        }
    }
}
